package com.hzlj.position.dbsync.liaoning;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.google.common.collect.Maps;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.TreeMap;

@Slf4j
public class JzryCompare {
    private static final Map<String, JzryOld> jzryOld = Maps.newHashMap();
    private static final Map<String, JzryNew> jzryNew = Maps.newHashMap();

    public static void main(String[] args) {
        String oldFile = "C:\\Users\\Administrator\\Desktop\\ln_old_1.xlsx";
        String newFile = "C:\\Users\\Administrator\\Desktop\\ln_new.xlsx";
        // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
        EasyExcel.read(oldFile, JzryOld.class, new ExcelListenerOld()).sheet().doRead();
        EasyExcel.read(newFile, JzryNew.class, new ExcelListenerNew()).sheet().doRead();
        System.out.println(jzryOld.size());
        System.out.println(jzryNew.size());

//        jzryNew.forEach((k,v)->{
//            if(!jzryOld.containsKey(k)){
//                System.out.println(v.getJgId()+","+v.getId()+","+v.getSfz()+","+v.getXm()+","+v.getJgId()+","+v.getJgMc());
//            }
//        });


        jzryOld.forEach((k,v)->{
            if(!jzryNew.containsKey(k)){
                System.out.println(v.getSfz()+","+v.getXm()+","+v.getSjh()+","+v.getCityJgMc()+","+v.getDistrictJgMc()+","+v.getSfsJgMc());
            }
        });

        TreeMap<String, String> value = Maps.newTreeMap();
        jzryNew.forEach((k,v)->{
            JzryOld old = jzryOld.get(k);
            if(old==null){
                return;
            }
            if(v.getJgMc()==null||old.getSfsJgMc()==null){
                return;
            }
            String oldJgmc = old.getSfsJgMc();
            oldJgmc=oldJgmc.replace(" ","");
            oldJgmc=oldJgmc.replace("前扬","前杨");
            oldJgmc=oldJgmc.replace("北镇大市司法所","北镇市大市司法所");
            oldJgmc=oldJgmc.replace("办事处","");
            oldJgmc=oldJgmc.replace("司法所","");
            oldJgmc=oldJgmc.replace("堡","");
//            oldJgmc=oldJgmc.replace("所","");
            oldJgmc=oldJgmc.replace("街道","");
            oldJgmc=oldJgmc.replace("镇","");
            oldJgmc=oldJgmc.replace("乡","");
            oldJgmc=oldJgmc.replace("地","");
            oldJgmc=oldJgmc.replace("子","");
            oldJgmc=oldJgmc.replace("营","");
            oldJgmc=oldJgmc.replace("所","");
            oldJgmc=oldJgmc.replace("农场","");
            oldJgmc=oldJgmc.replace("东六家","东六");
            oldJgmc=oldJgmc.replace("生","升");
            oldJgmc=oldJgmc.replace("北二十家","二十家");
            oldJgmc=oldJgmc.replace("大孤山","孤山");
            oldJgmc=oldJgmc.replace("宽邦","宽帮");
            oldJgmc=oldJgmc.replace("杨杖","杨家杖");
            oldJgmc=oldJgmc.replace("马杖房","马仗房");
            oldJgmc=oldJgmc.replace("阎店","闫店");
            oldJgmc=oldJgmc.replace("长兴岛交流岛","交流岛");
            oldJgmc=oldJgmc.replace("长兴岛长兴岛","长兴岛");
            oldJgmc=oldJgmc.replace("沙金台","沙金");
            oldJgmc=oldJgmc.replace("桓洞","恒洞");
            oldJgmc=oldJgmc.replace("柏家沟","栢家沟");
            oldJgmc=oldJgmc.replace("板石沟","板石");
            oldJgmc=oldJgmc.replace("平安西部","平西");
            oldJgmc=oldJgmc.replace("南芬办","南芬");
            oldJgmc=oldJgmc.replace("前福兴","福兴");
            oldJgmc=oldJgmc.replace("老大杖","老达杖");
            oldJgmc=oldJgmc.replace("台吉区","台吉");
            oldJgmc=oldJgmc.replace("田师付","田师傅");
            oldJgmc=oldJgmc.replace("西六","西六家子");
            oldJgmc=oldJgmc.replace("海洲乡","海州乡");
            if(oldJgmc.contains("市")){
                oldJgmc=oldJgmc.substring(oldJgmc.indexOf("市")+1);
            }
            if(oldJgmc.contains("区")){
                oldJgmc=oldJgmc.substring(oldJgmc.indexOf("区")+1);
            }
            if(oldJgmc.contains("县")){
                oldJgmc=oldJgmc.substring(oldJgmc.indexOf("县")+1);
            }
            if(oldJgmc.contains("局")){
                oldJgmc=oldJgmc.substring(oldJgmc.indexOf("局")+1);
            }

            String newJgMc = v.getJgMc();
            newJgMc=newJgMc.replace(" ","");
            newJgMc=newJgMc.replace("北镇大市司法所","北镇市大市司法所");
            newJgMc=newJgMc.replace("办事处","");
            newJgMc=newJgMc.replace("司法所","");
            newJgMc=newJgMc.replace("堡","");
//            newJgMc=newJgMc.replace("所","");
            newJgMc=newJgMc.replace("街道","");
            newJgMc=newJgMc.replace("镇","");
            newJgMc=newJgMc.replace("乡","");
            newJgMc=newJgMc.replace("地","");
            newJgMc=newJgMc.replace("子","");
            newJgMc=newJgMc.replace("营","");
            newJgMc=newJgMc.replace("所","");
            newJgMc=newJgMc.replace("农场","");
            newJgMc=newJgMc.replace("东六家","东六");
            newJgMc=newJgMc.replace("生","升");
            newJgMc=newJgMc.replace("北二十家","二十家");
            newJgMc=newJgMc.replace("大孤山","孤山");
            newJgMc=newJgMc.replace("宽邦","宽帮");
            newJgMc=newJgMc.replace("杨杖","杨家杖");
            newJgMc=newJgMc.replace("马杖房","马仗房");
            newJgMc=newJgMc.replace("阎店","闫店");
            newJgMc=newJgMc.replace("长兴岛交流岛","交流岛");
            newJgMc=newJgMc.replace("长兴岛长兴岛","长兴岛");
            newJgMc=newJgMc.replace("沙金台","沙金");
            newJgMc=newJgMc.replace("桓洞","恒洞");
            newJgMc=newJgMc.replace("柏家沟","栢家沟");
            newJgMc=newJgMc.replace("板石沟","板石");
            newJgMc=newJgMc.replace("平安西部","平西");
            newJgMc=newJgMc.replace("南芬办","南芬");
            newJgMc=newJgMc.replace("前福兴","福兴");
            newJgMc=newJgMc.replace("老大杖","老达杖");
            newJgMc=newJgMc.replace("台吉区","台吉");
            newJgMc=newJgMc.replace("田师付","田师傅");
            newJgMc=newJgMc.replace("西六","西六家子");
            newJgMc=newJgMc.replace("海洲乡","海州乡");
            if(newJgMc.contains("市")){
                newJgMc=newJgMc.substring(newJgMc.indexOf("市")+1);
            }
            if(newJgMc.contains("区")){
                newJgMc=newJgMc.substring(newJgMc.indexOf("区")+1);
            }
            if(newJgMc.contains("县")){
                newJgMc=newJgMc.substring(newJgMc.indexOf("县")+1);
            }
            if(newJgMc.contains("局")){
                newJgMc=newJgMc.substring(newJgMc.indexOf("局")+1);
            }

            if(!newJgMc.contains(oldJgmc)){
//                System.out.println(v.getId()+","+v.getSfz()+","+v.getXm()+","+v.getJgMc()+","+old.getCityJgMc()+","+old.getDistrictJgMc()+","+old.getSfsJgMc());
            }
        });
    }

    @Data
    public static class JzryOld {
        @ExcelProperty(value = "市司法局", index = 0)
        private String cityJgMc;
        @ExcelProperty(value = "县区司法局", index = 1)
        private String districtJgMc;
        @ExcelProperty(value = "所属单位", index = 2)
        private String sfsJgMc;
        @ExcelProperty(value = "姓名", index = 3)
        private String xm;
        @ExcelProperty(value = "身份证号", index = 4)
        private String sfz;
        @ExcelProperty(value = "手机号", index = 5)
        private String sjh;
        @ExcelProperty(value = "矫正开始时间", index = 6)
        private String jzksrq;
        @ExcelProperty(value = "矫正结束时间", index = 7)
        private String jzjsrq;
        @ExcelProperty(value = "状态", index = 8)
        private String zt;
    }


    @Data
    public static class JzryNew {
        @ExcelProperty(value = "id", index = 0)
        private String id;
        @ExcelProperty(value = "xm", index = 1)
        private String xm;
        @ExcelProperty(value = "sfz", index = 2)
        private String sfz;
        @ExcelProperty(value = "jg_code", index = 3)
        private String jgCode;
        @ExcelProperty(value = "jg_mc", index = 4)
        private String jgMc;
        @ExcelProperty(value = "jg_id", index = 5)
        private String jgId;
    }

    public static class ExcelListenerOld extends AnalysisEventListener<JzryOld> {
        /*一行一行的读取excel 中的内容，从第二行读取*/
        @Override
        public void invoke(JzryOld demoData, AnalysisContext analysisContext) {
            if (jzryOld.containsKey(demoData.getSfz())) {
//                log.warn("用户重复:sfz={},user={}", demoData.getSfz(), demoData);
                return;
            }
            if(ObjectUtil.isEmpty(demoData.getSfz())){
                return;
            }
            jzryOld.put(demoData.getSfz().toUpperCase(), demoData);
//            System.out.println("每一行的内容"+demoData);
        }

        /*读取表头中的内容*/
        @Override
        public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
            System.out.println("表头的内容为" + headMap);
        }

        /*读取完成之后执行的方法*/
        @Override
        public void doAfterAllAnalysed(AnalysisContext analysisContext) {

        }
    }


    public static class ExcelListenerNew extends AnalysisEventListener<JzryNew> {
        /*一行一行的读取excel 中的内容，从第二行读取*/
        @Override
        public void invoke(JzryNew demoData, AnalysisContext analysisContext) {
            if (jzryNew.containsKey(demoData.getSfz())) {
//                log.warn("用户重复:sfz={},user={}", demoData.getSfz(), demoData);
                return;
            }
            if(ObjectUtil.isEmpty(demoData.getSfz())){
                return;
            }
            jzryNew.put(demoData.getSfz().toUpperCase(), demoData);
//            System.out.println("每一行的内容"+demoData);
        }

        /*读取表头中的内容*/
        @Override
        public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
            System.out.println("表头的内容为" + headMap);
        }

        /*读取完成之后执行的方法*/
        @Override
        public void doAfterAllAnalysed(AnalysisContext analysisContext) {

        }
    }
}
